package com.cat.dynamicPlanning02;

import java.util.stream.IntStream;

/**
*   @description https://leetcode.cn/problems/partition-equal-subset-sum/
*   @author 曲大人的喵
*   @create 2025/11/5 18:09
*   @since JDK17
*/

public class Solution41 {
    public boolean canPartition(int[] nums) {
        int sum = IntStream.of(nums).sum(), target = sum / 2, n = nums.length;
        boolean[] dp = new boolean[target + 1];
        // dp[i] : 表示是否可以在数组中选取出刚好总价值为i的可能
        if (sum % 2 != 0) {
            return false;
        }
        dp[0] = true;
        for (int i = 0; i < n; i++) {
            for (int j = target; j >= nums[i]; j--) {
                dp[j] = dp[j] || dp[j - nums[i]];   // 不选这个数字，或者选这个数字
            }
        }

        return dp[target];
    }
}
